//
//  DEGTheDiscountPushViewController.m
//  LinLiSheQu
//
//  Created by 郭树清 on 2018/7/23.
//  Copyright © 2018年 KuoShuChing. All rights reserved.
//

#import "DEGTheDiscountPushViewController.h"
#import "HWTextView.h"

@interface DEGTheDiscountPushViewController ()<UITextViewDelegate>
@property (nonatomic,strong) HWTextView *discountTextView;
@property (nonatomic,strong) NSString *inofStr;
@property (nonatomic,strong) UILabel *numLabel;


@end

@implementation DEGTheDiscountPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"群公告";
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
    [self initTextViewInfoAndEvent];
}

- (void)initTextViewInfoAndEvent{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, NAVH,Device_Width, ZYScaleH(175))];
    bgView.backgroundColor = [UIColor whiteColor];
    self.discountTextView = [[HWTextView alloc]initWithFrame:CGRectMake(15,0, Device_Width-30, ZYScaleH(175))];
    self.discountTextView.delegate = self;
    self.discountTextView.alwaysBounceVertical = NO;
    self.discountTextView.textColor = Color_33;
    self.discountTextView.placeholderColor = Color_99;
    self.discountTextView.placeholder = @"输入您想推送的内容";
    self.discountTextView.font = [UIFont systemFontOfSize:16];
    [bgView addSubview:self.discountTextView];
    [self.view addSubview:bgView];
    
    //确认推送按钮
//    UIButton *surePushBtn = [UIButton ZYButtonWithTitle:@"确认推送" titleFont:[UIFont systemFontOfSize:16] Image:nil backgroundImage:nil backgroundColor:KAppDefaultColor titleColor:[UIColor whiteColor] frame:CGRectMake(15,bgView.bottom+30, Device_Width-30, 40)];
//    surePushBtn.layer.cornerRadius = 5;
//    surePushBtn.layer.masksToBounds = YES;
//
//    [surePushBtn addClickBlock:^(UIButton *button) {
//        [self pushToUserNewMessage];
//    }];
//    [self.view addSubview:surePushBtn];
    
    self.numLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.discountTextView.width-60, self.discountTextView.height-20, 60, 20)];
    self.numLabel.text = @"0/40";
    self.numLabel.textColor = Color_99;
    self.numLabel.textAlignment = NSTextAlignmentRight;
    self.numLabel.font = [UIFont systemFontOfSize:12];
    [self.discountTextView addSubview:self.numLabel];
}

- (void)textViewDidChange:(UITextView *)textView{
    
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    self.inofStr = textView.text;
    NSInteger existTextNum = self.inofStr.length;
    if (existTextNum >40)
    {
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [self.inofStr substringToIndex:40];
        
        [textView setText:s];
    }
    
    self.inofStr = textView.text;
    self.numLabel.text = [NSString stringWithFormat:@"%ld/40",self.inofStr.length];
}


#pragma mark   ------- 推送请求-------
/*
- (void)pushToUserNewMessage{
    [self.discountTextView resignFirstResponder];
    if (self.inofStr.length==0) {
            [[AlertHelper sharedManager] alertWithMessage:@"请输入推送信息"];
        return;
    }
    NSString *urlStr = [KSService pushCouponCall];
    NSMutableDictionary *pramseDic = [NSMutableDictionary dictionary];
    DELoginModel *model = [HHUserManager getUserInfo];
    [pramseDic setObject:model.userId forKey:@"userId"];
    [pramseDic setObject:model.tokenId forKey:@"tokenId"];
    [pramseDic setObject:self.customerID forKey:@"buyerId"];
    [pramseDic setObject:self.inofStr forKey:@"content"];
    [KSHTTPSTool post:urlStr params:pramseDic success:^(id responseObject) {
        NSInteger code = [responseObject[@"retCode"] integerValue];
        if (code == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [[AlertHelper sharedManager]alertWithMessage:[NSString stringWithFormat:@"%@",responseObject[@"retMsg"]]];
        }
    } failure:^(NSError *error) {
        
    }];
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
